home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / cport20.zip / CPORT.HPP < prev    next >
C/C++ Source or Header  |  1993-04-09  |  8KB  |  284 lines

  1. //
  2. // CPORT.HPP
  3. //
  4. // C++ Extention Header file for Cport Communications Library
  5. //
  6. // Copyright (c) 1993 Bri Productions
  7. //
  8. //
  9.  
  10.  
  11. #ifndef _CPORT_HPP_
  12. #define _CPORT_HPP_
  13.  
  14.  
  15. //-------------------------------------
  16. //
  17. // Cport class
  18. //
  19. //-------------------------------------
  20. class Cport
  21. {
  22.       // Default values
  23.       //
  24.    enum   {
  25.             DEF_RXQ  = 1024,
  26.             DEF_TXQ  = 512,
  27.             DEF_MODE = W8|S1|NONE
  28.          };
  29.  
  30.    int   open_err;   // open errors
  31.    COM   c;          // serial port handle
  32.    int*  ref_cnt;    // reference count
  33.  
  34. public:
  35.  
  36.       // Constructors and destructor
  37.       //
  38.    Cport(unsigned id, 
  39.          int baud, 
  40.          byte mode = DEF_MODE, 
  41.          unsigned rxQ = DEF_RXQ, 
  42.          unsigned txQ = DEF_TXQ,
  43.          byte htype = OFF
  44.          );
  45.    Cport(unsigned id, 
  46.          int baud, 
  47.          byte mode, 
  48.          unsigned rxQ, 
  49.          unsigned txQ,
  50.          byte htype,
  51.          unsigned thresh
  52.          );
  53.    Cport  (const CPARAM& param);
  54.    ~Cport ();
  55.  
  56.  
  57.       // Copy constructor and assignment
  58.       //
  59.    Cport            (Cport& cport);
  60.    Cport& operator= (Cport& cport);
  61.  
  62.  
  63.       // Typecasts
  64.       //
  65.    operator int()    { return(open_err); }
  66.    operator COM()    { return(c);        }
  67.    int ref()         { return(*ref_cnt); } // debug
  68.  
  69.  
  70.       // Control
  71.       //
  72.    void     Handshake (byte htype, unsigned thresh);
  73.    void     Param     (CPARAM& param);
  74.    void     Baud      (int baud);     
  75.    void     Mode      (byte mode);    
  76.    int      Tx        (int cmnd);  
  77.    void     NS550     (int trigger);  
  78.    static void Turbo  (int options);
  79.    unsigned RxQ       (unsigned size);
  80.    unsigned TxQ       (unsigned size);
  81.  
  82.  
  83.       // Input 
  84.       // 
  85.    char     Get     ();
  86.    unsigned LenRx   ();
  87.    char*    Get     (char *str, int maxc, char termc = '\n');
  88.    unsigned In      (void *abyte, unsigned nbyte);
  89.    void     FlushRx ();
  90.    char     Peek    ();
  91.    unsigned RxScan  (char ch);
  92.  
  93.  
  94.       // Output 
  95.       //
  96.    int      Put     (char _c);
  97.    unsigned LenTx   ();
  98.    int      Put     (const char *str);
  99.    unsigned Out     (const void *abyte, unsigned nbyte);
  100.    void     FlushTx ();
  101.    void     TxWait  ();
  102.  
  103.  
  104.       // Status 
  105.       //
  106.    unsigned Error  ();
  107.    unsigned Status ();
  108.    void     Out1   (byte on_off);
  109.    void     Rts    (byte on_off);
  110.    void     Dtr    (byte on_off);
  111.    int      Uart   ();
  112.  
  113.  
  114.       // Misc fuctions
  115.       //
  116.    void SetBreak ();
  117.    void ClrBreak ();
  118.    void Scratch  (byte abyte);
  119.    byte Scratch  ();
  120.  
  121.    static byte          Checksum (const void *abyte, unsigned nbyte);
  122.    static unsigned      Crc16    (const void *abyte, unsigned nbyte);
  123.    static unsigned long Crc32    (const void *abyte, unsigned nbyte);
  124.  
  125. };
  126.  
  127.  
  128. //-------------------------------------
  129. //
  130. // Constructors and destructor
  131. //
  132. //-------------------------------------
  133.  
  134. inline Cport::Cport(unsigned id, int baud, byte mode, 
  135.                                        unsigned rxQ, unsigned txQ, byte htype)
  136. {
  137.    *(ref_cnt = new int) = 1;
  138.    c = ComOpen(id, baud, mode, rxQ, txQ);
  139.    if((open_err = comopen_errno) == NO_ERR && htype != OFF)
  140.       ComHandshake(c, htype, (rxQ * 3) / 4);
  141. }
  142.  
  143. inline Cport::Cport(unsigned id, int baud, byte mode, 
  144.                      unsigned rxQ, unsigned txQ, byte htype, unsigned thresh)
  145. {
  146.    *(ref_cnt = new int) = 1;
  147.    c = ComOpen(id, baud, mode, rxQ, txQ);
  148.    if((open_err = comopen_errno) == NO_ERR && htype != OFF)
  149.       ComHandshake(c, htype, thresh);
  150. }
  151.  
  152. inline Cport::Cport(const CPARAM& param)
  153. {
  154.    *(ref_cnt = new int) = 1;
  155.    c = ComOpenS(¶m);
  156.    open_err = comopen_errno;
  157. }
  158.  
  159. inline Cport::~Cport()
  160. {
  161.    if(!(--(*ref_cnt)))
  162.    {
  163.       if(open_err == NO_ERR)
  164.          ComClose(c);
  165.       delete ref_cnt;
  166.    }
  167. }
  168.  
  169.  
  170. //-------------------------------------
  171. //
  172. // Copy constructor and assignment
  173. //
  174. //-------------------------------------
  175.  
  176. inline Cport::Cport(Cport& cport)
  177. {
  178.    open_err = cport.open_err;
  179.    c = cport.c;
  180.    (*(ref_cnt = cport.ref_cnt))++;
  181. }
  182.  
  183. inline Cport& Cport::operator=(Cport& cport)
  184. {
  185.    if(!(--(*ref_cnt)))
  186.     {
  187.       if(open_err == NO_ERR)
  188.           ComClose(c);
  189.       delete ref_cnt;
  190.     }
  191.  
  192.    open_err = cport.open_err;
  193.    c = cport.c;
  194.    (*(ref_cnt = cport.ref_cnt))++;
  195.    return(*this);
  196. }
  197.  
  198.  
  199. //-------------------------------------
  200. //
  201. // Control
  202. //
  203. //-------------------------------------
  204.  
  205. inline void     Cport::Handshake (byte htype, unsigned thresh)
  206.                                        { ComHandshake(c, htype, thresh); }
  207. inline void     Cport::Param (CPARAM& param) { ComParam(c, ¶m);      }
  208. inline void     Cport::Baud  (int baud)      { ComBaud(c, baud);         }
  209. inline void     Cport::Mode  (byte mode)     { ComMode(c, mode);         }
  210. inline int      Cport::Tx    (int on_off)    { return(ComTx(c, on_off)); }
  211. inline void     Cport::NS550 (int trigger)   { ComNS550(c, trigger);     }
  212. inline void     Cport::Turbo (int options)   { ComTurbo(options);        }
  213. inline unsigned Cport::RxQ   (unsigned size) { return(ComRxQ(c,size));    }
  214. inline unsigned Cport::TxQ   (unsigned size) { return(ComTxQ(c,size));    }
  215.  
  216.  
  217. //-------------------------------------
  218. //
  219. // Input 
  220. //
  221. //-------------------------------------
  222.  
  223. inline char     Cport::Get     ()                { return(ComGetc(c));       }
  224. inline unsigned Cport::LenRx   ()                { return(ComLenRx(c));      }
  225. inline char*    Cport::Get     (char *str, int max_c, char term_c) 
  226.                                { return(ComGets(c, str, max_c, term_c));     }
  227. inline unsigned Cport::In      (void *abyte, unsigned n_byte) 
  228.                                       { return(ComIn(c, abyte, n_byte));     }
  229. inline void     Cport::FlushRx ()                { ComFlushRx(c);            }
  230. inline char     Cport::Peek    ()                { return(ComPeek(c));       }
  231. inline unsigned Cport::RxScan  (char ch)         { return(ComRxScan(c, ch)); }
  232.  
  233.  
  234. //-------------------------------------
  235. //
  236. // Output
  237. //
  238. //-------------------------------------
  239.  
  240. inline int      Cport::Put     (char _c)         { return(ComPutc(c,_c ));  }
  241. inline unsigned Cport::LenTx   ()                { return(ComLenTx(c));     }
  242. inline int      Cport::Put     (const char *str) { return(ComPuts(c, str)); }
  243. inline unsigned Cport::Out     (const void *abyte, unsigned n_byte)
  244.                                    { return(ComOut(c, abyte, n_byte));      }
  245. inline void     Cport::FlushTx ()                { ComFlushTx(c);           }
  246. inline void     Cport::TxWait  ()                { ComTxWait (c);           }
  247.  
  248.  
  249. //-------------------------------------
  250. //
  251. // Status functions 
  252. //
  253. //-------------------------------------
  254.  
  255. inline unsigned Cport::Error   ()            { return(ComError(c));     }  
  256. inline unsigned Cport::Status  ()            { return(ComStatus(c));    }
  257. inline void     Cport::Out1    (byte on_off) { ComMcr(c, on_off, 0x04); }
  258. inline void     Cport::Rts     (byte on_off) { ComMcr(c, on_off, 0x02); }
  259. inline void     Cport::Dtr     (byte on_off) { ComMcr(c, on_off, 0x01); }
  260. inline int      Cport::Uart    ()            { return(ComUart(c));      }
  261.  
  262.  
  263. //-------------------------------------
  264. //
  265. // Misc fuctions
  266. //
  267. //-------------------------------------
  268.  
  269. inline void     Cport::SetBreak ()         { ComSetBreak (c);         }
  270. inline void     Cport::ClrBreak ()         { ComClrBreak (c);         }
  271. inline void     Cport::Scratch(byte abyte) { ComPutScrtch(c, abyte);  }
  272. inline byte     Cport::Scratch()           { return(ComGetScrtch(c)); }
  273.  
  274.  
  275. inline byte          Cport::Checksum (const void *abyte, unsigned nbyte)
  276.                                     { return(ComChecksum(abyte, nbyte)); }
  277. inline unsigned      Cport::Crc16    (const void *abyte, unsigned nbyte)
  278.                                     { return(ComCrc16(abyte, nbyte));    }
  279. inline unsigned long Cport::Crc32    (const void *abyte, unsigned nbyte)
  280.                                                { return(ComCrc32(abyte, nbyte));    }
  281.  
  282. #endif   // CPORT.HPP
  283.  
  284.